
//
//  GridView.m
//  TableTest
//
//  Created by 众拳科技 on 2017/11/14.
//  Copyright © 2017年 众拳科技. All rights reserved.
//

#import "GridView.h"
#import "Masonry.h"

@interface GridView()<FSCalendarDataSource,FSCalendarDelegate>

@property(nonatomic,strong)NSDate *selectDate;

@end

@implementation GridView

-(id)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        self.backgroundColor = CSWhiteColor;
        
        self.classType = @"团课";
        
        /**
         团课
         */
        UIButton *comClassBtn = [[UIButton alloc] init];
        [comClassBtn setTitle:@"团课"];
        comClassBtn.titleLabel.font = BoldBigFont;
        [comClassBtn setSelectedTitleColor:CSColorFromRGB(0x0a0a0a)];
        [comClassBtn setTitleColor:CSColorFromRGB(0xa5a5a5)];
        comClassBtn.tag = 0;
        [comClassBtn addTarget:self action:@selector(classBtnClick:)];
        comClassBtn.selected = YES;
        [self addSubview:comClassBtn];
        self.comClassBtn = comClassBtn;
        
        /**
         私教
         */
        UIButton *priClassBtn = [[UIButton alloc] init];
        [priClassBtn setTitle:@"私教"];
        priClassBtn.titleLabel.font = BoldBigFont;
        [priClassBtn setSelectedTitleColor:CSColorFromRGB(0x0a0a0a)];
        [priClassBtn setTitleColor:CSColorFromRGB(0xa5a5a5)];
        priClassBtn.hidden = YES;
        priClassBtn.tag = 1;
        [priClassBtn addTarget:self action:@selector(classBtnClick:)];
        [self addSubview:priClassBtn];
        self.priClassBtn = priClassBtn;
        
        /**
         线条
         */
        UIView *btnLineView = [[UIView alloc] init];
        btnLineView.backgroundColor = ButtonBackGroundColor;
        btnLineView.hidden = YES;
        [self addSubview:btnLineView];
        self.btnLineView = btnLineView;
        
//        UIButton *lookSelfBtn = [[UIButton alloc] init];
//        [lookSelfBtn setTitle:@"仅看自己"];
//        [lookSelfBtn setTitleColor:CSColorFromRGB(0x999999) forState:UIControlStateNormal];
//        [lookSelfBtn setTitleColor:CSColorFromRGB(0x999999) forState:UIControlStateSelected];
//        lookSelfBtn.titleLabel.font = MidFont;
//        [lookSelfBtn setImage:@"icn_open"];
//        [self addSubview:lookSelfBtn];
//        self.lookSelfBtn = lookSelfBtn;
        
        
        /**
         显示日期的表格
         */
        UIButton *timeBtn = [[UIButton alloc] init];
        [timeBtn setTitleColor: CSColorFromRGB(0x0a0a0a) forState:UIControlStateNormal];
        [timeBtn setTitleColor: CSColorFromRGB(0x0a0a0a) forState:UIControlStateSelected];
        [timeBtn addTarget:self action:@selector(timeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        timeBtn.titleLabel.font = BoldMidBigFont;
        [timeBtn setTitle:[self getCurrentTime] forState:UIControlStateNormal];
        [timeBtn setImage:@"icn_open"];
        [self addSubview:timeBtn];
        self.timeBtn = timeBtn;
        
        
        /**
         月份Label
         */
//        UILabel *monthLabel = [[UILabel alloc] init];
//        monthLabel.font = BoldMidBigFont;
//        monthLabel.textColor = CSColorFromRGB(0x0a0a0a);
//        monthLabel.text = [self getCurrentMonth];
//        [self addSubview:monthLabel];
//        self.monthLabel = monthLabel;
        
                
        //顶部日期选择
        DateCollectinView *dateCollectionView = [[DateCollectinView alloc] init];
        [self addSubview:dateCollectionView];
        self.dateCollectionView = dateCollectionView;

        
        WeekCollectionView *weekDayCollectionView = [[WeekCollectionView alloc] initWithFrame:CGRectZero];
        weekDayCollectionView.firstDayOfWeek = dateCollectionView.firstDayOfWeek;
        [self addSubview:weekDayCollectionView];
        self.weekDayCollectionView = weekDayCollectionView;
        
        
        /**
         今日按钮
         */
        UIButton *todayBtn = [[UIButton alloc] init];
        [todayBtn setImage:@"icn_today"];
        [todayBtn setHighlightedImage:@"icn_today"];
        [todayBtn addTarget:self action:@selector(todayBtnClick)];
        [self addSubview:todayBtn];
        self.todayBtn = todayBtn;
        
        TeachBottomView *teachBottomView = [[TeachBottomView alloc] init];
        [self addSubview:teachBottomView];
        self.teachBottomView = teachBottomView;
        
        [self setConstraints];
        
        //把FirstDayOfWeek 广播出去
         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(firstDayOfWeekChange:) name:@"WeekCollectionViewPageScorll" object:nil];
    }
    
    return self;
}

-(void)setConstraints{
    
    [self.comClassBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(self).offset(15);
        
        make.left.mas_equalTo(self).offset(12);
    }];
    
    
    [self.priClassBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self).offset(15);
        
        make.left.mas_equalTo(self.comClassBtn.mas_right).offset(22);
    }];
    
    [self.btnLineView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(self.comClassBtn);
        
        make.top.mas_equalTo(self.comClassBtn.mas_bottom).offset(15);
        
        make.width.mas_equalTo(self.comClassBtn);
        
        make.height.mas_equalTo(1.5);
    }];
    
//    [self.lookSelfBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
//
//        make.centerY.mas_equalTo(self.comClassBtn);
//
//        make.right.mas_equalTo(self).offset(-12);
//    }];
    
    /**
     显示日期的按钮
     */
    [self.timeBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.centerY.mas_equalTo(self.comClassBtn);
        
        make.right.mas_equalTo(self).offset(-20);
    }];
    
//    [self.monthLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
//
//        make.bottom.mas_equalTo(self.timeBtn).offset(-2);
//
//        make.left.mas_equalTo(self).offset(12);
//    }];
    
    [self.dateCollectionView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.comClassBtn.mas_bottom).offset(10);
        
        make.right.mas_equalTo(self);
        
        make.left.mas_equalTo(self).offset(SCREEN_WIDTH / 8);
        
        make.height.mas_equalTo(70);
    }];
    
    
    [self.todayBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.centerY.mas_equalTo(self.dateCollectionView);
        
        make.left.mas_equalTo(self).offset(12);
    }];
    
    [self.weekDayCollectionView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.dateCollectionView.mas_bottom);
        
        make.left.width.bottom.mas_equalTo(self);
    }];
    
    [self.teachBottomView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(self).offset(-25);
        
        make.bottom.mas_equalTo(self).offset(-30);
    }];
}

-(void)layoutSubviews{
    
    [self.timeBtn imageOnTheTitleRightWithSpace:6];
    
    //[self.lookSelfBtn imageOnTheTitleRightWithSpace:6];
}


/**
 今日按钮点击
 */
-(void)todayBtnClick{
    
    self.dateCollectionView.selectDate = [NSDate date];
    
    [self.timeBtn setTitle:[self getCurrentTime]];
    
    self.monthLabel.text = [self getCurrentMonth];
}


-(void)classBtnClick:(UIButton *)sender{
    
    NSInteger tag = sender.tag;
    
    if (tag == 0) {
        
        [self.btnLineView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.left.mas_equalTo(self.comClassBtn);
            
            make.top.mas_equalTo(self.comClassBtn.mas_bottom).offset(15);
            
            make.width.mas_equalTo(self.comClassBtn);
            
            make.height.mas_equalTo(1.5);
        }];
        
        self.classType = @"团课";
    }
    else{
        
        [self.btnLineView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.left.mas_equalTo(self.priClassBtn);
            
            make.top.mas_equalTo(self.comClassBtn.mas_bottom).offset(15);
            
            make.width.mas_equalTo(self.comClassBtn);
            
            make.height.mas_equalTo(1.5);
        }];
        
        self.classType = @"私教";
    }
    
    self.comClassBtn.selected = NO;
    
    self.priClassBtn.selected = NO;
    
    sender.selected = !sender.selected;
    
    [NotificationCenter postNotificationName:@"GridClassTypeChange" object:nil];
}

/**
 选择时间按钮点击
 */
-(void)timeBtnClick:(UIButton *)btn{
    
    btn.selected = !btn.selected;
    
    if (btn.selected) {
        
        FSCalendar *calendar = [[FSCalendar alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(btn.frame), self.frame.size.width, 0)];
        calendar.dataSource = self;
        calendar.delegate = self;
        calendar.today = nil;
        calendar.backgroundColor = [UIColor whiteColor];
        calendar.appearance.headerMinimumDissolvedAlpha = 0;
        calendar.appearance.headerTitleFont = [UIFont systemFontOfSize:12];
        calendar.appearance.weekdayTextColor = [UIColor blackColor];
        calendar.appearance.weekdayFont = [UIFont systemFontOfSize:12];
        calendar.appearance.caseOptions = FSCalendarCaseOptionsHeaderUsesUpperCase;
        if (self.selectDate) {
            
            [calendar selectDate:self.selectDate];
            [calendar selectDate:self.selectDate scrollToDate:YES];
        }
        else{
            
            [calendar selectDate:[NSDate date]];
            [calendar selectDate:[NSDate date] scrollToDate:YES];
        }
        calendar.alpha = 0;
        [self addSubview:calendar];
        self.calendar = calendar;
        [UIView animateWithDuration:0.25 animations:^{
           
            calendar.alpha = 1;
            
            CGRect frame = calendar.frame;
            
            frame.size.height = 200;
            
            calendar.frame = frame;
        }];
    }
    else{
        
        [self.calendar removeFromSuperview];
        
        self.calendar = nil;
    }
}

#pragma FSCalendarDelegate

- (void)calendar:(FSCalendar *)calendar didSelectDate:(NSDate *)date atMonthPosition:(FSCalendarMonthPosition)monthPosition{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    
    [formatter setDateFormat:@"yyyy年-MM月"];
    
    NSString *currentTimeString = [formatter stringFromDate:date];
    
    [self.timeBtn setTitle:currentTimeString];
    
//    [formatter setDateFormat:@"MM月"];
//
//    currentTimeString = [formatter stringFromDate:date];
//
//    self.monthLabel.text = currentTimeString;
    
    [self.calendar removeFromSuperview];
    
    self.calendar = nil;
    
    NSLog(@"%@",currentTimeString);
    
    self.selectDate = date;
    
    self.dateCollectionView.selectDate = date;
}


-(void)setGridType:(NSString *)gridType{
    
    _gridType = gridType;
    
    self.weekDayCollectionView.gridType = gridType;
}

#pragma mark 日期视图滑动的通知
-(void)firstDayOfWeekChange:(NSNotification *)sender{
    
    NSDictionary *userInfo = sender.userInfo;
    
    NSDate *date = [userInfo objectForKey:@"firstDayOfWeek"];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    
    [formatter setDateFormat:@"yyyy年-MM月"];
    
    NSString *currentTimeString = [formatter stringFromDate:date];
    
    [self.timeBtn setTitle:currentTimeString];
    
    //[formatter setDateFormat:@"MM月"];
    
    //currentTimeString = [formatter stringFromDate:date];
    
    //self.monthLabel.text = currentTimeString;
}

-(NSString *)getCurrentTime{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制 年-MM月
    
    [formatter setDateFormat:@"yyyy年-MM月"];
    
    //现在时间,你可以输出来看下是什么格式
    
    NSDate *datenow = [NSDate date];
    
    //----------将nsdate按formatter格式转成nsstring
    
    NSString *currentTimeString = [formatter stringFromDate:datenow];
    
    return currentTimeString;
}

-(NSString *)getCurrentMonth{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制 年-MM月
    
    [formatter setDateFormat:@"MM月"];
    
    //现在时间,你可以输出来看下是什么格式
    
    NSDate *datenow = [NSDate date];
    
    //----------将nsdate按formatter格式转成nsstring
    
    NSString *currentTimeString = [formatter stringFromDate:datenow];
    
    return currentTimeString;
}

-(void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
